package com.eqdd.yiqidian.ui.gongneng.renshi.kaoqin.rule;

import android.content.Intent;
import android.databinding.DataBindingUtil;
import android.os.Bundle;
import android.support.v7.widget.RecyclerView;
import android.text.TextUtils;
import android.view.View;

import com.afollestad.materialdialogs.MaterialDialog;
import com.eqdd.annotation.datasave.ToSave;
import com.eqdd.yiqidian.AddRuleActivityCustom;
import com.eqdd.yiqidian.R;
import com.eqdd.yiqidian.base.Config;
import com.eqdd.yiqidian.bean.rebuild.EmploymentListResultBean;
import com.eqdd.yiqidian.bean.rebuild.RuleListResultBean;
import com.eqdd.yiqidian.bean.rebuild.slimedit.SlimEditVEnterBean;
import com.eqdd.yiqidian.http.DialogCallBack;
import com.eqdd.yiqidian.http.HttpConfig;
import com.eqdd.yiqidian.http.HttpResult;
import com.eqdd.yiqidian.ui.base.SlimBaseMultiEditActivity;
import com.eqdd.yiqidian.ui.gongneng.renshi.kaoqin.classes.ChooseClassesActivity;
import com.eqdd.yiqidian.ui.select.SelectMultiStaffActivity;
import com.eqdd.yiqidian.utils.ToastUtil;
import com.lzy.okgo.OkGo;
import com.lzy.okgo.model.Response;

import java.io.Serializable;
import java.util.ArrayList;
import java.util.List;

public class UpdateRuleActivity extends SlimBaseMultiEditActivity {
    private AddRuleActivityCustom dataBinding;
    private MaterialDialog weekChoose;
    String shiftId;
    ArrayList<EmploymentListResultBean> employmentListResultBeans;
    String guids;
    private MaterialDialog holidayChoose;
    private RuleListResultBean bean;
    private String msg;

    @Override
    public void initBinding() {
        isRead = false;
        isSave = false;
        dataBinding = DataBindingUtil.setContentView(this, R.layout.activity_add_rule);
        initTopTitleBar(View.VISIBLE, "规则");
        initTopRightText("修改", v -> {
            if (checkParam()) {
                OkGo.<HttpResult>post(HttpConfig.BASE_URL + HttpConfig.UPDATE_RULE_INFO)
//                        .params("ruleName", setParam(0))
                        .params("userGuid", user.getGuid())
                        .params("companyId", user.getCompanyId())
                        .params("shiftId", shiftId)
                        .params("ruleShiftId", bean.getId())
//                        .params("ruleDescribe", " ")
//                        .params("objecter", guids)
                        .params("weeks", setParam(3))
                        .params("holidays", setParam(4))
                        .execute(new DialogCallBack<HttpResult>(UpdateRuleActivity.this) {
                            @Override
                            public void onSuccess(Response<HttpResult> response) {
                                HttpResult httpResult = response.body();
                                ToastUtil.showShort(httpResult.getMsg());
                                if (httpResult.getStatus() == 200) {
                                    bean.setHolidays(setParam(4));
                                    bean.setWeeks(setParam(3));
                                    bean.setShiftId(Integer.parseInt(shiftId));
                                    bean.setShiftName(setParam(1));
                                    Intent intent = new Intent();
                                    intent.putExtra("bean", bean);
                                    setResult(Config.SUCCESS, intent);
                                    finish();
                                }
                            }
                        });
            } else {
                ToastUtil.showShort(msg);
            }
        });
    }

    private boolean checkParam() {
        if (TextUtils.isEmpty(setParam(0))) {
            msg = "班别名称不能为空";
            return false;
        }
        if (TextUtils.isEmpty(setParam(1))) {
            msg = "班次未选择";
            return false;
        }
        if (TextUtils.isEmpty(setParam(3))) {
            msg = "上班周期未选择";
            return false;
        }
        return true;
    }


    @Override
    protected RecyclerView initRecyclerView() {
        return dataBinding.recyclerView;
    }

    @Override
    public void itemClick(int p) {

        switch (slimAdapterEx.getDataItem(p).getPosition()) {
            case 1:
                startActivityForResult(ChooseClassesActivity.class, null, Config.CHOOSE_CLASSES);
                break;
            case 2:
                Bundle bundle = new Bundle();
                bundle.putParcelableArrayList("list", employmentListResultBeans);
                startActivityForResult(SelectMultiStaffActivity.class, bundle, Config.SELECT_MULTI_STAFF);
                break;
            case 3:
                if (weekChoose == null) {
                    weekChoose = new MaterialDialog.Builder(this)
                            .title("上班周期选择")
                            .items("星期一", "星期二", "星期三", "星期四", "星期五", "星期六", "星期日")
                            .itemsCallbackMultiChoice(null, (dialog, which, text) -> {
                                StringBuilder stringBuilder = new StringBuilder();
                                for (CharSequence charSequence : text) {
                                    stringBuilder.append(charSequence).append(",");
                                }
                                if (text.length == 0) {
                                    slimAdapterEx.getData().get(3).setContent("");
                                    slimAdapterEx.notifyItemChanged(3);
                                    return true;
                                }
                                slimAdapterEx.getData().get(3).setContent(stringBuilder.substring(0, stringBuilder.lastIndexOf(",")));
                                slimAdapterEx.notifyItemChanged(3);
                                return true;
                            })
                            .positiveText("确定").build();
                }
                weekChoose.show();
                break;
            case 4:
                if (holidayChoose == null) {
                    holidayChoose = new MaterialDialog.Builder(this)
                            .title("上班周期选择")
                            .canceledOnTouchOutside(true)
                            .items("元旦", "春节", "清明节", "劳动节", "端午节", "国庆节", "中秋节")
                            .itemsCallbackMultiChoice(null, (dialog, which, text) -> {
                                StringBuilder stringBuilder = new StringBuilder();
                                for (CharSequence charSequence : text) {
                                    stringBuilder.append(charSequence).append(",");
                                }
                                if (text.length == 0) {
                                    slimAdapterEx.getData().get(4).setContent(" ");
                                    slimAdapterEx.notifyItemChanged(4);
                                    return true;
                                }
                                slimAdapterEx.getData().get(4).setContent(stringBuilder.substring(0, stringBuilder.lastIndexOf(",")));
                                slimAdapterEx.notifyItemChanged(4);
                                return true;
                            })
                            .positiveText("确定").build();
                }
                holidayChoose.show();
                break;

        }

    }


    @Override
    protected void setEditBeans() {


    }

    @Override
    public void setView() {
        employmentListResultBeans = new ArrayList<>();
        bean = getIntent().getParcelableExtra("bean");
        shiftId = bean.getShiftId() + "";
        datas.add(new SlimEditVEnterBean("班别名称", bean.getRuleName(), 0, "请输入"));
        datas.add(new SlimEditVEnterBean("班次", bean.getShiftName(), 1, true, "请选择"));
        datas.add(new SlimEditVEnterBean("适应人员", "", 2, true, "请选择"));
        datas.add(new SlimEditVEnterBean("设置上班周期", bean.getWeeks(), 3, true, "请选择"));
        datas.add(new SlimEditVEnterBean("设置节假日", bean.getHolidays(), 4, true, "请选择"));
        slimAdapterEx.updateData(datas);
        OkGo.<HttpResult<List<EmploymentListResultBean>>>post(HttpConfig.BASE_URL + HttpConfig.RULE_INFO_STAFF_LIST)
                .params("ruleShiftId", bean.getId())
                .execute(new DialogCallBack<HttpResult<List<EmploymentListResultBean>>>(UpdateRuleActivity.this) {
                    @Override
                    public void onSuccess(Response<HttpResult<List<EmploymentListResultBean>>> response) {
                        HttpResult<List<EmploymentListResultBean>> httpResult = response.body();
                        ToastUtil.showShort(httpResult.getMsg());
                        if (httpResult.getStatus() == 200) {
                            datas.get(2).setContent(httpResult.getItems().size() + "人");
                            employmentListResultBeans = (ArrayList<EmploymentListResultBean>) httpResult.getItems();
                            slimAdapterEx.notifyItemChanged(2);
                        }
                    }
                });
    }

    @Override
    public void onClick(View v) {
        super.onClick(v);
        switch (v.getId()) {

        }
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        if (requestCode == Config.CHOOSE_CLASSES && resultCode == Config.SUCCESS) {
            shiftId = data.getStringExtra(Config.SHIFTID);
            String content = data.getStringExtra("content");
            slimAdapterEx.getDataItem(1).setContent(content);
            slimAdapterEx.notifyItemChanged(1);
        } else if (requestCode == Config.SELECT_MULTI_STAFF && resultCode == Config.INTENT_CONTENT) {
            employmentListResultBeans = data.getParcelableArrayListExtra("list");
            String content = data.getStringExtra("content");
            guids = data.getStringExtra("guids");
            slimAdapterEx.getDataItem(2).setContent(content);
            slimAdapterEx.notifyItemChanged(2);
            updateStaff();
        } else {
            super.onActivityResult(requestCode, resultCode, data);
        }

    }

    private void updateStaff() {
        OkGo.<HttpResult>post(HttpConfig.BASE_URL + HttpConfig.UPDATE_RULE_INFO_STAFF_LIST)
                .params("userGuid", user.getGuid())
                .params("companyId", user.getCompanyId())
                .params("ruleShiftId", bean.getId())
                .params("objecter", guids)
                .execute(new DialogCallBack<HttpResult>(UpdateRuleActivity.this) {
                    @Override
                    public void onSuccess(Response<HttpResult> response) {
                        HttpResult httpResult = response.body();
                        ToastUtil.showShort(httpResult.getMsg());
                        if (httpResult.getStatus() == 200) {

                        }
                    }
                });
    }
}
